package com.cn.test.ui.example;

import android.Manifest;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.cn.test.MainActivity;
import com.cn.test.R;
import com.cn.test.base.BaseActivity;
import com.cn.test.model.ScanBean;
import com.cn.test.model.response.UserInfoBean;
import com.cn.test.mvp.presenter.LoginUserPresenter;
import com.cn.test.mvp.presenter.view.LoginUserView;
import com.tbruyelle.rxpermissions2.RxPermissions;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2017/7/3.
 * do what to 2017/7/3
 */

public class ScanActivity extends BaseActivity<LoginUserView, LoginUserPresenter> implements LoginUserView, QRCodeView.Delegate {
    @BindView(R.id.zxingview)
    ZXingView mZXingView;
    @Override
    protected int getNormalLayoutId() {
        return R.layout.activity_scan_info;
    }

    @Override
    protected int getErrorLayoutId() {
        return R.layout.activity_scan_info;
    }

    @Override
    protected LoginUserPresenter createPresenter() {
        return new LoginUserPresenter(this, this);
    }

    @Override
    protected void initViewsAndEvents() {
        initPermission();
        mZXingView.setDelegate(this);
    }
    private void initPermission() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(
                Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE
        ).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    onStart();
                } else {
                    //只要有一个权限被拒绝，就会执行
                    Toast.makeText(ScanActivity.this, "请先同意权限", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    @Override
    public void onLogin(UserInfoBean resultBean) {

    }

    @Override
    public void requestFailed(String message) {

    }
    @Override
    protected void onStart() {
        super.onStart();
        mZXingView.startCamera(); // 打开后置摄像头开始预览，但是并未开始识别
//        mZXingView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT); // 打开前置摄像头开始预览，但是并未开始识别
        mZXingView.startSpotAndShowRect(); // 显示扫描框，并开始识别
        mZXingView.startSpot(); // 开始识别
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    private static final String TAG = "ScanActivity";
    @Override
    public void onScanQRCodeSuccess(String result) {
        Log.i(TAG, "result:" + result);
        setTitle("扫描结果为：" + result);
        vibrate();
//        mZXingView.startSpot(); // 开始识别
        EventBus.getDefault().post(new ScanBean(result));
        finish();
    }
    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }
    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {

    }

    @Override
    public void onScanQRCodeOpenCameraError() {

    }


    @Override
    protected void onStop() {
        mZXingView.stopCamera(); // 关闭摄像头预览，并且隐藏扫描框
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mZXingView.onDestroy(); // 销毁二维码扫描控件
        super.onDestroy();
    }
}
